package up.android.gps;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class GPSActivity extends Activity implements LocationListener {

	private LocationManager locationManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	
		locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		locationManager.requestLocationUpdates
			(android.location.LocationManager.GPS_PROVIDER,15000,0,this);

	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		locationManager.removeUpdates(this);
	}

	public void onLocationChanged(Location loc) {
		Geocoder geoCoder = new Geocoder(
	            getApplicationContext(), Locale.getDefault());
	        try {
	        	List<Address> aux = 
	             geoCoder.getFromLocation(
	                loc.getLatitude(), 
	                loc.getLongitude(), 5);
	        	if(aux != null && aux.size() > 0)
	        	{
	        		for(Address a : aux)
	        		{
	        			String s = "";
	        	        for (int i=0; i<a.getMaxAddressLineIndex();i++)
	        	        	s += a.getAddressLine(i) + "\n";
	        	        Toast toast = Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG);
	        	        toast.show();
	        		}
	        	}
	        }
	        catch (IOException e) {                
	            Log.e("POC",e.getMessage());
	        }
		
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
}
